package starComponent.controls;

import controls.DataControl;
import data.visu.structures.PairOfDouble;
import starComponent.E_PossibleBranches;
import starComponent.elements.A_StarBranch;
import starComponent.elements.StarBranchBiSlider;
import starComponent.elements.StarBranchBinaries;
import starComponent.elements.StarBranchSlider;
import starComponent.elements.Thumb;
import starComponent.elements.ThumbForMultipleBinaries;

public class BranchValuesObserver implements ObserverThumb{

	private StarComponentControler starControler;

	private DataControl dataControl;

	public BranchValuesObserver(StarComponentControler starControler, DataControl dataControl){
		this.starControler = starControler;
		this.dataControl = dataControl;
		listenThumbsToObserve();
	}


	protected void listenThumbsToObserve() {
		for(A_StarBranch b:starControler.getDisplayedBranchlist()){
			if(b instanceof StarBranchSlider){
				((StarBranchSlider)b).getThumb().addObserverThumb(this);
			}
			else if (b instanceof StarBranchBiSlider){
				((StarBranchBiSlider)b).getThumbStart().addObserverThumb(this);
				((StarBranchBiSlider)b).getThumbEnd().addObserverThumb(this);
			}
			else if(b instanceof StarBranchBinaries){
				for(ThumbForMultipleBinaries t:((StarBranchBinaries)b).getThumbsTab()){
					t.addObserverThumb(this);
				}
			}
		}
	}





	@Override
	public void update(Thumb thumb) {

		switch(thumb.getBranch().getBranchType().getType()){
		case E_PossibleBranches.QUANTITATIVE_BINARY:
			break;

		case E_PossibleBranches.QUANTITATIVE_SLIDER:
			dataControl.getParamsControl().updateParamsMap(thumb.getBranch().getBranchType(), new Double(thumb.getValue()));
			break;

		case E_PossibleBranches.QUANTITATIVE_BISLIDER:
			double startValue = ((StarBranchBiSlider)(thumb.getBranch())).getThumbStart().getValue();
			double endValue = ((StarBranchBiSlider)(thumb.getBranch())).getThumbEnd().getValue();
			dataControl.getParamsControl().updateParamsMap(thumb.getBranch().getBranchType(), new PairOfDouble(startValue, endValue));
			break;

		case E_PossibleBranches.QUALITATIVE_BINARY:
			break;

		case E_PossibleBranches.QUALITATIVE_SLIDER:
			dataControl.getParamsControl().updateParamsMap(thumb.getBranch().getBranchType(), new Integer((int) thumb.getValue()));
			break;

		case E_PossibleBranches.QUALITATIVE_BISLIDER:
			break;

		default:
			throw new IllegalArgumentException("thumb de type inconnu");
		}

	}

}
